package leetcode.list;

import utils.node.ListNode;

/**
 * @Description: https://leetcode.cn/problems/rotate-list/description/
 * @Author Ammar
 * @Create 2023/8/3 11:49
 */
public class _61_旋转链表 {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null) return head;
        if (k == 0) return head;
        ListNode node = head;
        int n = 1;
        while(node.next != null) {
            node = node.next;
            n++;
        }

        int add = n - (k % n);
        if (add == 0) return head;

        node.next = head;

        while(add != 0) {
            node = node.next;
            add--;
        }
        head = node.next;
        node.next = null;
        return head;
    }
}
